package cn.cmdr.service.ServiceImpl;

import cn.cmdr.mapper.CreditMapper;
import cn.cmdr.mapper.SupplierMapper;
import cn.cmdr.pojo.Credit;
import cn.cmdr.pojo.Supplier;
import cn.cmdr.service.SupplierService;
import cn.cmdr.vo.ComVo;
import cn.cmdr.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service("supplierService")
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {
    @Resource
    private  SupplierMapper supplierMapper;

    @Resource
    private CreditMapper creditMapper;

    @Override
    public List<Supplier> getSupplier(Integer brandId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(brandId!=null,"brand_id",brandId);
        return supplierMapper.selectList(queryWrapper);
    }

    @Override
    public PageVo getSupPage(Integer pageNo, Integer pageSize,Supplier supplierVo) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(supplierVo.getBrandId()!=null,"brand_id",supplierVo.getBrandId())
                .like(!StringUtils.isEmpty(supplierVo.getName()),"s.name",supplierVo.getName())
                .eq(supplierVo.getCreditId()!=null,"credit_id",supplierVo.getCreditId());
        List<Supplier> list = supplierMapper.getSupPage(page,queryWrapper);
        return new PageVo(list,page.getTotal());
    }

    @Override
    public void add(Supplier supplier) {
        supplierMapper.insert(supplier);
    }

    @Override
    public List<Credit> getCredit() {
        return creditMapper.selectList(null);
    }

    @Override
    public void updateSupplier(Supplier supplier) {
        supplierMapper.updateById(supplier);
    }


}
